/**
 *
 */
package com.ean.backpack.dto;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;

import com.ean.backpack.core.util.CoreUtils;
import com.ean.backpack.core.util.RoleUtils;
import com.ean.backpack.domain.Role;
import com.ean.backpack.domain.User;

/**
 * @author valentin
 *
 */
@Configurable
public class UserDto {
	private String firstName;
	private String lastName;
	private String email;
	private String password;
	private String verifyPassword;

	//TODO check why doesn't work with
	@Autowired
	private RoleUtils roleUtils;

	public User createUser() {
		User user = new User();
		user.setFirstName(getFirstName());
		user.setLastName(getLastName());
		user.setEmail(getEmail());
		user.setPassword(CoreUtils.getEncodedPassword(getPassword()));

		Role userRole = roleUtils.getRole("ROLE_USER");
		List<Role> roles = new ArrayList<Role>();
		roles.add(userRole);
		user.setRoles(roles);
		user.setIsEnabled(true);
		return user;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getVerifyPassword() {
		return verifyPassword;
	}

	public void setVerifyPassword(String verifyPassword) {
		this.verifyPassword = verifyPassword;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((email == null) ? 0 : email.hashCode());
		result = prime * result
				+ ((firstName == null) ? 0 : firstName.hashCode());
		result = prime * result
				+ ((lastName == null) ? 0 : lastName.hashCode());
		result = prime * result
				+ ((password == null) ? 0 : password.hashCode());
		result = prime * result
				+ ((verifyPassword == null) ? 0 : verifyPassword.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		UserDto other = (UserDto) obj;
		if (email == null) {
			if (other.email != null)
				return false;
		} else if (!email.equals(other.email))
			return false;
		if (firstName == null) {
			if (other.firstName != null)
				return false;
		} else if (!firstName.equals(other.firstName))
			return false;
		if (lastName == null) {
			if (other.lastName != null)
				return false;
		} else if (!lastName.equals(other.lastName))
			return false;
		if (password == null) {
			if (other.password != null)
				return false;
		} else if (!password.equals(other.password))
			return false;
		if (verifyPassword == null) {
			if (other.verifyPassword != null)
				return false;
		} else if (!verifyPassword.equals(other.verifyPassword))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "UserDto [firstName=" + firstName + ", lastName=" + lastName
				+ ", email=" + email + ", password=" + password
				+ ", verifyPassword=" + verifyPassword + "]";
	}

}
